package com.beiming.kotlindemo

/**
 * Created by lingchen on 2020/4/30. 16:36
 * mail:lingchen52@foxmail.com
 */
class LearnKotlinObject {

    fun main() {
        val p = Person("Jack", 19)
//        p.name = "Jack"
//        p.age = 19
        p.eat()

        val student1 = Student()
        val student2 = Student("Jack", 19)
        val student3 = Student("a123", 5, "Jack", 19)
        student3.eat()

        doStudy(student3)

        testData()

        Singleton.singletonTest()

        printUpperCase()

        printParams(123)

        //键值对传参  可忽略参数顺序
        printParams(num = 123, str = "hello")
        printParams(str = "hello", num = 123)
        printParams(str = "hello")
        printParams(num = 123)

    }

    private fun doStudy(study: Study?) {
        study?.readBook()
        study?.doHomeWork()

        //let函数  stu和study是同一对象
        study?.let { stu ->
            stu.readBook()
            stu.doHomeWork()
        }

        //简化
        study?.let {
            it.readBook()
            it.doHomeWork()
        }
    }

    private fun testData() {
        val cellphone1 = Cellphone("Samsung", 1299.99)
        val cellphone2 = Cellphone("Sumsung", 1299.99)
        println(cellphone1)
        println("cellphone1 equals cellphone2 " + (cellphone1 == cellphone2))

    }

    private fun getTextLength(text: String): Int {
        if (text != null) {
            return text.length
        }
        return 0
    }

    //简化  ?:操作符 如果左边表达式的结果不为空就返回左边表达式的结果，否则就返回右边表达式的结果
    private fun getTextLength1(text: String?) = text?.length ?: 0


    var content: String? = "hello"

    private fun testNotNullAssert() {
        if (content != null) {
            printUpperCase()
        }
    }

    //非空断言 !!
    private fun printUpperCase() {
        val upperCase = content!!.toUpperCase()
        println(upperCase)
    }

    //函数的参数默认值
    private fun printParams(num: Int = 100, str: String = "hello") {
        println("num is $num , str is $str")
    }

}